// pages/shoujiao/shoujiao.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modeIndex: 0,
    modeList: ['全部', '已读', '未读'],
    commentList: [],
	guanggao:[],
    currentIndex: '',
    dateEnd: '',
    isReadMode: true,
    isNotReadMode: true,
    isShowModel: false,
    currentAvatar: '',
    currentName: '',
    currentContent: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.page = 1
    that.maxCommentListLength = 999999
    that._getCommentList().then((res) => {
      that.maxCommentListLength = res.data.data.total
      that.setData({
        commentList: res.data.data.comment_list,
		guanggao: res.data.data.guanggao
      })
    })





    // 获取当前日期，设置为可选择的最后日期
    let nowDate = new Date()
    this.setData({dateEnd: `${nowDate.getFullYear()}-${nowDate.getMonth() + 1}-${nowDate.getDate()}`})
    this.nickname = wx.getStorageSync('userInfo').wxInfo.nickName
  },
  selectDate (e) {
    let that = this
    that.date = e.detail.value
    that.page = 1
    that._getCommentList(e.detail.value).then((res) => {
      console.log(res)
      that.setData({
        commentList: res.data.data.comment_list
      })
    })
  },
  cancelDate () {
    let that = this
    that.page = 1
    that.date = undefined
    that._getCommentList().then((res) => {
      console.log(res)
      that.setData({
        commentList: res.data.data.comment_list
      })
    })
  },
  bindModeChange: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      modeIndex: e.detail.value
    })
    that.mode = e.detail.value
    that.page = 1
    that._getCommentList(e.detail.value).then((res) => {
      console.log(res)
      that.setData({
        commentList: res.data.data.comment_list
      })
    })
  },
  showContent (e) {
    // console.log('openIndex: ', this.openIndex)
    // 如果当前块的举报按钮打开就返回
    // if (this.openIndex !== undefined) {
    //   this.clearOpen()
    // }
    // 如果未读，上传已读的状态
    if (e.currentTarget.dataset.isread == 0) {
      this._read (e.currentTarget.dataset.id)
      this.setData({[`commentList[${e.currentTarget.dataset.index}].is_read`]: 1})
    }
    // 如果当前块已经展开就把他关掉
    // if (e.currentTarget.dataset.index === this.data.currentIndex && this.data.currentIndex !== '') {
    //   this.setData({
    //     currentIndex: ''
    //   })
    //   return
    // }
    this.showModel(e.currentTarget.dataset)
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  onShareAppMessage: function () {
    return {
      title: `${this.nickname}邀你吐槽他`,
      path: `booleans_zhijiao/index/index?id=${app.globalData.i_id}`,
      imageUrl: "../../images/sample.jpg"
    }
  },
  onPageScroll () {
    // 滚动时关掉所有打开的块,有bug废弃
    // this.setData({
    //   currentIndex: ''
    // })
    // 滚动时关掉打开的举报按钮
    // if (this.openIndex !== undefined) {
    //   this.setData({
    //     [`commentList[${this.openIndex}].right`]: 0,
    //     [`commentList[${this.openIndex}].isDelShow`]: false
    //   })
    //   this.openIndex = undefined
    // }
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    let that = this
    console.log('--------上拉刷新-------')
    that.page = 1
    that._getCommentList().then((res) => {
      console.log(res)
      that.setData({
        commentList: res.data.data.comment_list
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }).catch((res) => {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    })
  },
  onReachBottom () { // 下拉加载
    console.log('--------下拉加载-------')
    let that = this
    // 显示加载图标
    console.log('current: ', that.data.commentList.length, ' total: ', that.maxCommentListLength)
    if (that.data.commentList.length < that.maxCommentListLength) {
      that.page += 1
    } else {
      wx.showToast({
        title: '无更多受槽',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    that._getCommentList().then((res) => {
      let result = that.data.commentList
      result = result.concat(res.data.data.comment_list)
      that.setData({
        commentList: result
      })
      wx.hideLoading()
    })
  },
  acceptComment (e) {
    let that = this
    // console.log(e.currentTarget.dataset.id)
    that._acceptComment(e.currentTarget.dataset.id).then((res) => {
      console.log(res)
      wx.showToast({
        title: res.data.message,
        icon: 'none'
      })
      that.setData({[`commentList[${e.currentTarget.dataset.index}].is_accept`]: 1})
    })
  },




/********************************************************* by booleans*/
  linkxcx () {
		app.util.request({
			url: 'entry/wxapp/guanggao',
			data: {
			  page: 0
			},
			success: (res) => {
				console.log(res);
				wx.navigateToMiniProgram({
				  appId: res.data.data[0].appid,
				  path: res.data.data[0].linkxcx,
				  extraData: {
					foo: 'bar'
				  },
				  //envVersion: 'develop',
				  success(res1) {
					// 打开成功
					console.log(res1)
				  }
				})
			},
			fail: (res) => {
			  console.log(res)
			}
		})
  },






  showMenu (e) {
    let that = this
    let id = e.currentTarget.dataset.id
    // console.log(e.currentTarget.dataset.id)
    wx.showActionSheet({
      itemList: ['删除', '举报'],
      success: (res) => {
        if (res.tapIndex === 0) {
          // console.log('删除')
          this._delete(id).then((res) => {
            console.log(res)
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
            that.page = 1
            that.date = undefined
            that._getCommentList().then((res) => {
              console.log(res)
              that.setData({
                commentList: res.data.data.comment_list
              })
            })
          }).catch((res) => {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
            that.page = 1
            that.date = undefined
            that._getCommentList().then((res) => {
              console.log(res)
              that.setData({
                commentList: res.data.data.comment_list
              })
            })
          })
        } else if (res.tapIndex === 1) {
          // console.log('举报')
          this._report(id).then((res) => {
            console.log(res)
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }).catch((res) => {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          })
        }
      },
      fail: function(res) {
        console.log(res.errMsg)
      }
    })
  },
  showModel (data) {
    // console.log(data)
    this.setData({
      isShowModel: true,
      currentAvatar: data.avatar,
      currentName: data.name,
      currentContent: data.content
    })
  },
  hideModel() {
    this.setData({
      isShowModel: false,
      currentAvatar: '',
      currentName: '',
      currentContent: ''
    })
  },













  _getCommentList () {
    // 获取受教列表
    let that = this
    return new Promise ((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/commentList',
        data: {
          page: that.page,
          mode: that.mode,
          that_day: that.date
        },
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },
  _acceptComment (id) {
    let that = this
    return new Promise ((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/acceptcomment',
        data: {
          comment_id: id
        },
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },
  _read (id) {
    app.util.request({
      url: 'entry/wxapp/readcomment',
      data: {
        comment_id: id
      },
      success: (res) => {
        console.log(res)
      }
    })
  },
  _report (id) {
    return new Promise ((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/accusecomment',
        data: {
          comment_id: id
        },
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },
  _delete (id) {
    return new Promise ((resolve, reject) => {
      app.util.request({
        url: 'entry/wxapp/deletecomment',
        data: {
          comment_id: id
        },
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  }
})
